package com.ds.dsm.view.nav.service.grid;

import com.ds.common.JDSException;
import com.ds.config.ListResultModel;
import com.ds.dsm.view.config.grid.GridMenuView;
import com.ds.esd.custom.annotation.GridViewAnnotation;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.bean.ApiClassConfig;
import com.ds.esd.custom.bean.MethodConfig;
import com.ds.esd.custom.enums.CustomMenuItem;
import com.ds.esd.custom.grid.CustomGridViewBean;
import com.ds.esd.custom.grid.enums.GridMenu;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.view.ViewEntityConfig;
import com.ds.web.util.PageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/dsm/view/config/grid/")
public class ViewGridToolbarService {


    @RequestMapping(method = RequestMethod.POST, value = "ToolBars")
    @GridViewAnnotation(
            addPath = "ToolBarsTree")
    @ModuleAnnotation(caption = "工具栏",
            imageClass = "spafont spa-icon-c-toolbar")
    @APIEventAnnotation(autoRun = true, bindMenu = CustomMenuItem.treeNodeEditor)
    @ResponseBody
    public ListResultModel<List<GridMenuView>> getToolBars(String sourceClassName, String methodName, String domainId, String viewInstId) {
        ListResultModel<List<GridMenuView>> resultModel = new ListResultModel<>();
        try {
            ViewEntityConfig classConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);
            ApiClassConfig customESDClassAPIBean = classConfig.getSourceConfig();
            MethodConfig methodAPIBean = customESDClassAPIBean.getMethodByName(methodName);
            CustomGridViewBean customGridViewBean = (CustomGridViewBean) methodAPIBean.getView();
            Set<GridMenu> customMenu = customGridViewBean.getToolBarMenu();
            List<GridMenuView> views = new ArrayList<>();
            for (GridMenu formMenu : customMenu) {
                views.add(new GridMenuView(formMenu, methodAPIBean));
            }

            resultModel = PageUtil.getDefaultPageList(views);
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return resultModel;
    }


}
